package threadState;

/**
 * 演示线程状态切换
 */
public class ThreadStateDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("====== Thread1 NEW -> RUNNABLE -> TERMINATED =====================");
        Thread t1 = new Thread(() -> {
            System.out.println("thread1 running");
            System.out.println("thread1 state -> " + Thread.currentThread().getState());
            System.out.println("thread1 exit");
        });
        System.out.println("thread1 state -> " + t1.getState());
        System.out.println("调用thread1.start()");
        t1.start();
        Thread.sleep(200);
        System.out.println("main sleep 200");
        System.out.println("thread1 state -> " + t1.getState());

        System.out.println("\n\n====== Thread2 NEW -> RUNNABLE -> TIMED_WAITING -> RUNNABLE -> TERMINATED =====================");
        Thread t2 = new Thread(() -> {
            System.out.println("thread2 running");
            System.out.println("thread2 state -> " + Thread.currentThread().getState());
            try {
                System.out.println("thread2 sleep 1000");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread2 state -> " + Thread.currentThread().getState());
            System.out.println("thread2 exit");
        });
        System.out.println("thread2 state -> " + t2.getState());
        System.out.println("调用thread2.start()");
        t2.start();
        Thread.sleep(200);
        System.out.println("main sleep 200");
        System.out.println("thread2 state -> " + t2.getState());
        Thread.sleep(1000);
        System.out.println("main sleep 1000");
        System.out.println("thread2 state -> " + t2.getState());

        System.out.println("\n\n====== Thread3 NEW -> RUNNABLE -> BLOCKED -> RUNNABLE -> TERMINATED =====================");
        Object monitorLock = new Object();
        Thread t3 = new Thread(() -> {
            System.out.println("thread3 running");
            synchronized (monitorLock) {
                System.out.println("thread3 get monitor");
                System.out.println("thread3 state -> " + Thread.currentThread().getState());
            }
            System.out.println("thread3 exit");
        });
        System.out.println("thread3 state -> " + t3.getState());
        synchronized (monitorLock) {
            System.out.println("调用thread3.start()");
            t3.start();
            System.out.println("main yield");
            Thread.yield();
            System.out.println("thread3 state -> " + Thread.currentThread().getState());
            Thread.sleep(200);
            System.out.println("main sleep 200");
            System.out.println("thread3 state -> " + t3.getState());
        }
        Thread.sleep(1000);
        System.out.println("main sleep 1000");
        System.out.println("thread3 state -> " + t3.getState());
    }
}
